﻿using System;
using UnityEngine;
using UnityEngine.UI;

namespace Jinndev.UI {

    public class DialogUI : BaseUIBehaviour, IEscape {

        public static string PATH = "Jinndev/Prefab/UI/Common/DialogUI";

        /// <summary>
        /// 创建自定义弹窗
        /// </summary>
        /// <param name="title"></param>
        /// <param name="message"></param>
        /// <param name="onYes"></param>
        /// <param name="onNo"></param>
        /// <param name="onClose"></param>
        /// <returns></returns>
        public static DialogUI Create(string title, string message, Action onYes = null, Action onNo = null, Action onClose = null) {
            DialogUI dialogUI = UIManager.Instance.Add<DialogUI>(PATH, new UIParam { modal = true, mask = true, docb = false });
            dialogUI.Init(title, message, onYes, onNo, onClose);
            return dialogUI;
        }

        /// <summary>
        /// 创建带有一个确认按钮的信息弹窗
        /// </summary>
        /// <param name="title"></param>
        /// <param name="message"></param>
        /// <param name="onYes"></param>
        /// <param name="onClose"></param>
        /// <returns></returns>
        public static DialogUI CreateInfo(string title, string message, Action onYes = null, Action onClose = null) {
            return Create(title, message, onYes, null, onClose);
        }

        /// <summary>
        /// 创建带有确认、取消按钮的确认弹窗
        /// </summary>
        /// <param name="title"></param>
        /// <param name="message"></param>
        /// <param name="onYes"></param>
        /// <param name="onNo"></param>
        /// <param name="onClose"></param>
        /// <returns></returns>
        public static DialogUI CreateConfirm(string title, string message, Action onYes = null, Action onNo = null, Action onClose = null) {
            return Create(title, message, onYes, onNo, onClose).No(true);
        }

        /// <summary>
        /// 创建带有一个确认按钮的错误信息弹窗
        /// </summary>
        /// <param name="title"></param>
        /// <param name="error"></param>
        /// <param name="onYes"></param>
        /// <param name="onClose"></param>
        /// <returns></returns>
        public static DialogUI CreateError(object error, Action onYes = null, Action onClose = null) {
            string message;
            if (error == null) {
                message = "Unknown error";
            }
            else if (error is Exception) {
                message = (error as Exception).Message;
            }
            else {
                message = error.ToString();
            }
            return Create("Error", "<color=red>" + message + "</color>", onYes, null, onClose);
        }

        public Text textTitle;
        public Text textMessage;
        public GameObject buttonYes;
        public GameObject buttonNo;

        private Action onYes;
        private Action onNo;
        private Action onClose;

        private void Awake() {
            //onInvokeDestroy += () => {
            //    onClose?.Invoke();
            //};
        }

        private void OnDestroy() {
            onClose?.Invoke();
        }

        public DialogUI Init(string title, string message, Action onYes = null, Action onNo = null, Action onClose = null) {
            this.onYes += onYes;
            this.onNo += onNo;
            this.onClose += onClose;
            textTitle.text = title;
            textMessage.text = message;
            No(onNo != null);
            return this;
        }

        public DialogUI No(bool show) {
            buttonNo.gameObject.SetActive(show);
            return this;
        }


        public void Yes(string text) {
            SetText(buttonYes, "Text", text);
        }

        public void No(string text) {
            SetText(buttonNo, "Text", text);
            No(true);
        }

        public void OnClickYes() {
            Destroy();
            onYes?.Invoke();
        }

        public void OnClickNo() {
            Destroy();
            onNo?.Invoke();
        }

        public EscapeResult OnEscape() {
            OnClickNo();
            return EscapeResult.Destroy;
        }

    }

}
